package com.linlong.linlongchat.controller;


import com.linlong.linlongchat.domain.dto.*;
import com.linlong.linlongchat.domain.po.Result;
import com.linlong.linlongchat.domain.po.User;
import com.linlong.linlongchat.service.IFriendService;
import com.linlong.linlongchat.service.IUserService;
import com.linlong.linlongchat.utils.Md5Util;
import com.linlong.linlongchat.utils.ThreadLocalUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Api(tags = "用户接口")
public class UserController {


    @Autowired
    private IUserService userService;

    /**
     * 发送验证码
     *  /user/sendEmail?email=xxx  GET请求
     *  @author yhj
     * @param email
     * @return
     */
    @GetMapping("/sendEmail")
    @ApiOperation("注册时发送验证码")
    public Result<String> sendEmail(String email) {
        return userService.sendEmail(email);
    }
    /**
     * 重置密码发送验证码
     *  /user/sendEmail?email=xxx  GET请求
     *  @author yhj
     * @param email
     * @return
     */
    @GetMapping("/sendEmailResest")
    @ApiOperation("登录和重置密码时发送验证码")
    public Result<String> sendEmailResest(String email) {
        return userService.sendEmailResest(email);
    }
    /**
     * 账号密码登录
     * /user/login  username=xxx&password=xxx  POST请求
     * @author yhj
     * @return 登录结果
     */
    @PostMapping("/login")
    @ApiOperation("账号密码登录")
    public Result<String> login(@RequestBody UserLoginDto userLoginDto) {
        return userService.login(userLoginDto.getAccount(), userLoginDto.getPassword());
    }
    /**
     * 退出登录
     * /user/logout
     * @author lzh
     * @return 登录结果
     */
    @PostMapping("/logout")
    @ApiOperation("退出登录")
    public Result logout(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        System.out.println("token:" + token);

        return userService.logout(token);
    }


    /**
     * 邮箱登录
     * /user/emailLogin  email=xxx&code=xxx  POST请求
     * @author rht
     * @return 登录结果
     */
    @PostMapping("/emailLogin")
    @ApiOperation("邮箱登录")
    public Result<String> emailLogin(@RequestBody UserEmailLoginDto userEmailLoginDto) {
        return userService.emailLogin(userEmailLoginDto.getEmail(), userEmailLoginDto.getCode());
    }
    /**
     * 更改用户名
     * /user/updateUsername  username=xxx&password=xxx  PUT请求
     * @author rht
     * @return 修改结果
     */
    @PutMapping("/updateUsername")
    @ApiOperation("修改用户名")
    public Result<String> updateUsername(  @RequestBody UserUpdateNaDto userUpdateNaDto ) {
        return userService.updateUsername(userUpdateNaDto.getAccount(),userUpdateNaDto.getUsername());
    }
    /**
     * 更改个性签名
     * /user/updateSignature  signature=xxx  username=xxx&password=xxx  PUT请求
     * @author rht
     * @return 修改结果
     */
    @PutMapping("/updateSignature")
    @ApiOperation("修改个性签名")
    public Result updateSignature(@RequestBody UserUpdateSigDto userUpdateSigDto) {
//        Map<String, Object> claims= ThreadLocalUtil.get();
//        String account = (String)claims.get("account");
        return userService.updateSignature(userUpdateSigDto.getAccount(),userUpdateSigDto.getSignature());
    }
    /**
     * 修改性别
     * /user/updateSex/{sex}
     * @author lzh
     * @return 修改结果
     */
    @PutMapping("/updateSex/{sex}")
    @ApiOperation("修改性别")
    public Result updateSex(@PathVariable("sex") Integer sex) {
        Map<String, Object> claims= ThreadLocalUtil.get();
        String account = (String)claims.get("account");
        return userService.updateSex(account,sex);
    }


    /**
     * 忘记密码
     * /user/reset-password  email=xxx&newPassword=xxx&inputCode=xxx POST请求
     * @author rht
     * @return 成功或失败提示
     */
    @PostMapping("/resetPassword")
    @ApiOperation("忘记密码")
    public Result<String> resetPassword(@RequestBody UserResetPwd userResetPwd) {
            return userService.updatePassword(userResetPwd.getEmail(), userResetPwd.getNewPassword(), userResetPwd.getInputCode());
        }
    /**
     * 用户注册方法
     * /user/register  nickName=xxx&email=xxx&code=xxx&password=xxx  POST请求
     * @author yhj
     * @return
     */
    @PostMapping("/register")
    @ApiOperation("注册")
    @ResponseBody
    public Result<String> register(@RequestBody UserRegisterDto userRegisterDto) {
        return userService.register(userRegisterDto.getUsername(), userRegisterDto.getEmail(), userRegisterDto.getCode(), userRegisterDto.getPassword());
    }

    /**
     * 根据账号查用户信息
     * /user/getUserByAccount/{account}   GET请求
     * @author web
     * @return
     */
    @GetMapping("/getUserByAccount/{account}")
    @ApiOperation("根据账号查用户信息")
    public Result<User> getUserByAccount(@PathVariable("account") String account) {
        return userService.getUserByAccount(account);
    }
    /**
     * 获取当前用户信息
     * /user/getSelf   GET请求
     * @author web
     * @return
     */
    @GetMapping("/getSelf")
    @ApiOperation("获取当前用户信息")
    public Result<User> getSelf() {
        Map<String,Object> claim= ThreadLocalUtil.get();
        String account = (String) claim.get("account"); //当前登录用户的ID
        return userService.getUserByAccount(account);
    }
    }

